﻿using System;
using System.Collections.Generic;
using Gameplay.PVE.Utils;
using UnityEngine;
using Yoozoo.Libs;
using Yoozoo.Managers;
using Yoozoo.UI.YGUI;

namespace Gameplay.PVE.Survivor
{
    public class HudBubble : IClass
    {
        private BattleHudObject hudObject;

        private GameObject root;
        private YImage icon;
        private BattleUnit unit;
        private Animation animation;

        public void Create(BattleUnit unit)
        {
            this.unit = unit;
            if (BattleDebug.enableLog)
            {
                BattleDebug.Log("Create Hud " + unit.unitId);
            }
            hudObject = BattlePoolManager.Instance.GetHud((int)EBattleResId.BubbleHud);
            hudObject.SetPrefabReadyAction(o =>
            {
                
                o.transform.SetParent(unit.Transform);
                o.transform.localPosition = new Vector3(0, 2.3f, 0);
                o.transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);
                //root = o.transform.Find("bg").gameObject;
                //root.SetActive(false);
                icon = o.transform.Find("bg/icon").GetComponent<YImage>();
                if (this.unit.isDead)
                {
                    return;
                }
                animation = o.GetComponentInChildren<Animation>();
                if (animation != null)
                {
                    animation.Play();
                }
            });
        }

        public void Show()
        {
            if (hudObject != null)
            {
                hudObject.Forward = CameraManager.Instance.CameraForward;
                hudObject.Active = true;
                
            }
        }

        public void Hide()
        {
            if (hudObject != null)
            {
                hudObject.Active = false;
            }
        }

        public void Destroy()
        {
            if (hudObject != null)
            {
                if (BattleDebug.enableLog)
                {
                    BattleDebug.Log("Destroy Hud " + unit.unitId);
                }
                BattlePoolManager.Instance.ReleaseHud(hudObject);
                hudObject = null;
            }
        }

        public void OnReset()
        {
            Destroy();
        }
    }
}
